﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.SqlRepositoryLibrary.Converters;
using System.Data.SqlClient;
using MovieRental.GeneralDataExceptions;
using MovieRental.DataInterfaces;

namespace MovieRental.SqlRepositoryLibrary
{
    public class SqlTemporaryDiscountRepository : SqlRepository<TemporaryDiscountInfo, Guid, TemporaryDiscountConverter>, ITemporaryDiscountRepository
    {
        protected SqlDiscountRepository _baseRepository;

        public SqlTemporaryDiscountRepository(string connectionString)
            : base(connectionString)
        {
            _baseRepository = new SqlDiscountRepository(_connectionString);
        }

        protected override bool Contains(TemporaryDiscountInfo entity)
        {
            return Contains(entity.Id);
        }

        public override void Delete(TemporaryDiscountInfo entity)
        {
            Delete(entity.Id);
        }

        public override void Delete(Guid id)
        {
            base.Delete(id);
            if (_baseRepository.Contains(id))
                _baseRepository.Delete(id);
        }

        public override void Save(TemporaryDiscountInfo entity)
        {
            _baseRepository.Save(entity);
            base.Save(entity);
        }

        public override TemporaryDiscountInfo Load(Guid id)
        {
            var discount = base.Load(id);
            discount.Value = _baseRepository.Load(id).Value;
            return discount;
        }

        public override IEnumerable<TemporaryDiscountInfo> LoadAll()
        {
            var discounts = new List<TemporaryDiscountInfo>(base.LoadAll());
            for (int i = 0; i < discounts.Count; i++)
            {
                var baseDiscount = _baseRepository.Load(discounts[i].Id);
                discounts[i].Value = baseDiscount.Value;
            }
            return discounts;
        }
    }
}
